package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.FieldValOptionVo;
import com.zb.entity.ZjCodeUserRegisterFieldVal;
import com.zb.entity.ZjCodeUserRegisterFieldValOption;
import com.zb.excel.CodeUserRegisterFieldValExtend;
import com.zb.mapper.ZjCodeUserRegisterFieldValMapper;
import com.zb.service.ZjCodeUserRegisterFieldValOptionService;
import com.zb.service.ZjCodeUserRegisterFieldValService;
import com.zb.vo.CodeUserRegisterFieldValVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 编码用户登记字段值表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-11-06
 */
@Service
public class ZjCodeUserRegisterFieldValServiceImpl extends ServiceImpl<ZjCodeUserRegisterFieldValMapper, ZjCodeUserRegisterFieldVal> implements ZjCodeUserRegisterFieldValService {
    @Resource
    private ZjCodeUserRegisterFieldValOptionService codeUserRegisterFieldValOptionService;

    // 保存登记自定义字段
    @Override
    public void save(Long custAccId, Long codeUserRegisterId, List<CodeUserRegisterFieldValVo> registerFieldVals) {
        if (CollectionUtil.isEmpty(registerFieldVals)) {
            return;
        }
        codeUserRegisterFieldValOptionService.deleteByCodeUserRegisterId(codeUserRegisterId);
        List<ZjCodeUserRegisterFieldVal> adds = new ArrayList<>();
        List<ZjCodeUserRegisterFieldVal> updates = new ArrayList<>();
        ZjCodeUserRegisterFieldVal fieldVal = null;
        List<ZjCodeUserRegisterFieldValOption> addOptVals = new ArrayList<>();
        ZjCodeUserRegisterFieldValOption option = null;
        for (CodeUserRegisterFieldValVo vo : registerFieldVals) {
            fieldVal = new ZjCodeUserRegisterFieldVal();
            BeanUtil.copyProperties(vo, fieldVal);
            if (null != fieldVal.getId()) {
                updates.add(fieldVal);
            } else {
                fieldVal.setId(IdWorker.getId());
                fieldVal.setCustAccId(custAccId);
                fieldVal.setCodeUserRegisterId(codeUserRegisterId);
                adds.add(fieldVal);
            }
            for (FieldValOptionVo optVo : vo.getOptVos()) {
                option = new ZjCodeUserRegisterFieldValOption();
                BeanUtil.copyProperties(optVo, option);
                option.setId(IdWorker.getId());
                option.setCustAccId(custAccId);
                option.setCodeUserRegisterId(codeUserRegisterId);
                option.setCodeUserRegisterFieldValId(fieldVal.getId());
                addOptVals.add(option);
            }
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
        if (CollectionUtil.isNotEmpty(addOptVals)) codeUserRegisterFieldValOptionService.saveBatch(addOptVals);
    }

    // 根据编码用户登记表ID获取登记字段信息
    @Override
    public List<CodeUserRegisterFieldValExtend> getByCodeUserRegisterId(Long codeUserRegisterId) {
        QueryWrapper<ZjCodeUserRegisterFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("id", "type", "name");
        wrapper.eq("code_user_register_id", codeUserRegisterId);
        List<ZjCodeUserRegisterFieldVal> fieldValList = this.list(wrapper);

        List<ZjCodeUserRegisterFieldValOption> options = codeUserRegisterFieldValOptionService.getByCodeUserRegisterFieldValIds(fieldValList.stream().map(ZjCodeUserRegisterFieldVal::getId).toList());

        return fieldValList.stream().map(fieldVal -> {
            CodeUserRegisterFieldValExtend extend = new CodeUserRegisterFieldValExtend();
            BeanUtil.copyProperties(fieldVal, extend);
            extend.setOptVos(options.stream().filter(f -> Objects.equals(f.getCodeUserRegisterFieldValId(), fieldVal.getId())).map(opt -> {
                return new FieldValOptionVo(opt.getId(), opt.getOptValue());
            }).toList());
            return extend;
        }).toList();
    }
}
